package com.dt.dubai.mech.controller;

import com.alibaba.fastjson.JSON;
import com.dt.dubai.core.exception.PageErrorMesg;
import com.dt.dubai.mech.entity.MechCouponCustomerEntity;
import com.dt.dubai.mech.controller.innate.MechCouponCustomerInnateAbs;
import com.dt.dubai.core.comm.*;
import com.dt.dubai.core.utile.DateTools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/*
* 顾客与优惠券关联表
*/

@Controller
@RequestMapping(value = "/mech/mechCouponCustomer")
public class MechCouponCustomerController extends MechCouponCustomerInnateAbs {

    /*innateServiceStart*/
    @Override
    protected void userSavEntity(MechCouponCustomerEntity inEntity, final Long userId) {
        inEntity.setDelSign(false);
        inEntity.setCustomerLoginRecordId(userId);
    }

    @Override
    protected void userModEntity(MechCouponCustomerEntity inEntity, final Long userId) {
    }

    @Override
    protected void userDelByKey(List<Long> recordIds, final Long userId) {
    }

    @Override
    protected void userQueByKey(Long recordId, Map<String, Object> map, final Long userId) {
        if (null != map && map.size() > 0) {
            map.put("createTxStamp", DateTools.dateFormat((Date) map.get("createTxStamp"), DateTools.DATE_FORMAT_YMD));
            map.remove("customerLoginRecordId");
            map.remove("couponRecordId");
            map.remove("customerLoginRecordId");
            map.remove("delSign");
            map.remove("lastupTxStamp");
        }
    }

    @Override
    protected void userQuePageList(PageParameterData queryCondition, List<PageCondition> joinCondition, List<Map<String, Object>> resultMap, final Long userId) {
        for (Map<String, Object> map : resultMap) {
            map.put("createTxStamp", DateTools.dateFormat((Date) map.get("createTxStamp"), DateTools.DATE_FORMAT_YMD));
            Boolean yesUse = null == map.get("yesUse") ? false : (Boolean) map.get("yesUse");
            if (yesUse) {
                map.put("yesUse", yesUse);
                map.put("yesUseStr", "已使用");
            } else {
                map.put("yesUse", yesUse);
                map.put("yesUseStr", "未使用");
            }
            map.remove("customerLoginRecordId");
            map.remove("delSign");
            map.remove("lastupTxStamp");
        }
    }

    /*innateServiceEnd*/
    /*以上内容请误删除或是移动，系统升级使用，用户代码请写入下方*/

    //用户领取优惠券
//    @RequestMapping(value = "/receiveCuppon", method = RequestMethod.POST)
//    @ResponseBody
//    public PageData<String> receiveCuppon(@RequestParam("data")  String data, final Long userId){
//        try {
//            SqlUtils.clearSqlCode(data);
//            SqlUtils.clearWebCode(data);
//            MechCouponCustomerEntity entity = JSON.parseObject(data, MechCouponCustomerEntity.class);
//            userSavEntity(entity, userId);
//            mechCouponCustomerSer.savEntity(entity, userId);
//            return new PageData().success(entity.getRecordId(), i18nUtile.getMessage(PageErrorMesg.E0.name()));
//        } catch (RuntimeException e) {
//            String code = e.getMessage() == null ? "E911" : e.getMessage().startsWith("E") ? e.getMessage() : "E911";
//            System.out.println(i18nUtile.getMessage(code));
//            e.printStackTrace();
//            return PageData.error(code, i18nUtile.getMessage(code));
//        }
//    }
}